package br.usp.pcs.basys.engine;

import br.usp.pcs.basys.Job;
import br.usp.pcs.basys.SystemParemeters;
import br.usp.pcs.basys.event.Event;
import br.usp.pcs.basys.event.MemoryRequest;

public abstract class Engine {

	IResources resources = Resources.getInstance();

	public void run() {
		SystemParemeters parameters = readSystemAndJobParameters();
		setupSystem(parameters);
		createEventList(parameters);
		executeEvents();
		printStatistics();
	}

	private void setupSystem(SystemParemeters parameters) {
		Memory memory = new Memory();
		memory.setMemorySize(parameters.getMemorySize());
		resources.setMemory(memory);
	}

	private void printStatistics() {
		// TODO Auto-generated method stub

	}

	private void executeEvents() {
		Event nextEvent = resources.getNextEvent();
		while (nextEvent!=null){
			nextEvent.execute();
			nextEvent = resources.getNextEvent();
		}
	}

	private void createEventList(SystemParemeters parameters) {
		for (Job job : parameters.getJobs()) {
			MemoryRequest event = new MemoryRequest(job, job
					.getArrivalDate(),resources);
			resources.add(event);
		}
	}

	abstract SystemParemeters readSystemAndJobParameters();
}
